<template>
	<view class="commodity bg-color">
		<view @tap="goDetail(item.id)" class="commodity-item" v-for="item in commodityList" :key="item.id">
			<image class="commodity-cover" :src="item.cover" mode=""></image>
			<view class="commodity-content">
				<text class="commodity-title">{{ item.name }}</text>
				<view class="quick-attrs" v-if="item.quickShowAttr">
					<view class="quick-attr-item" v-for="(attr,index) in item.quickShowAttr" :key="index">
						{{ attr }}
					</view>
				</view>
				<view class="price-box">
					<text class="current-price">
						<text class="price-prefix">¥</text>
						{{ item.price.toString().split(".")[0] }}</text>
						<text class="price-suffix">.{{ item.price.toString().split(".")[1] }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $http from '../../common/api/request.js'
	export default {
		props: {
			categoryId: ''
		},
		data() {
			return {
				commodityList: [],
				queryParams: {
					pageNo: 1,
					pageSize: 6
				},
				total: -1
			}
		},
		created() {
			this.listData()
		},
		methods: {
			goDetail(id) {
				uni.navigateTo({
					url: '../../pages/product/product?skuId=' + id
				})
			},
			listData() {
				if(this.commodityList.length === this.total) {
					return
				}
				if(this.categoryId) {
					this.queryParams.categoryId = this.categoryId
				}
				$http.request({
					url: '/product/sku/find',
					method: 'get',
					data: this.queryParams
				}).then(res => {
					this.commodityList.push(...res.data.records)
					this.total = res.data.total
				})
			}
		}
	}
</script>

<style scoped>
	.commodity {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		padding: 20rpx;
	}

	.commodity-item {
		padding-bottom: 20rpx;
		width: 48%;
		background-color: #fff;
		border-radius: 20rpx;
		overflow: hidden;
		margin-bottom: 30rpx;
	}

	.quick-attrs {
		width: 100%;
		display: flex;
		flex-wrap: nowrap;
		justify-content: flex-start;
	}

	.quick-attr-item {
		padding: 3rpx 8rpx;
		font-size: 16rpx;
		background-color: #F2F2F7;
		color: #A4A4A4;
		white-space: nowrap;
		overflow: hidden;
		max-width: 80rpx;
		text-align: center;
		margin: 0 5rpx;
		text-overflow: ellipsis;
	}

	.price-box {
		display: inline-block;
		margin-top: 10rpx;
		font-weight: 600;
		font-size: 20rpx;
	}

	.price-prefix {
		font-size: 20rpx;
	}

	.price-suffix {
		font-size: 22rpx;
		color: #FF4142;
	}

	.commodity-cover {
		width: 100%;
		height: 370rpx;
	}

	.commodity-title {
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		word-break: break-all;
	}

	.commodity-content {
		padding: 16rpx 20rpx;
	}

	.current-price {
		font-size: 32rpx;
		color: #FF4142;
	}

	.discount {
		border-radius: 4rpx;
		border: 1px solid #FF3333;
		padding: 2rpx 10rpx;
		font-size: 20rpx;
		color: #FF3333;
		margin-left: 10rpx;
	}
</style>
